package cn.iocoder.yudao.module.system.service.workhanging;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.system.dal.dataobject.workorder.WorkOrderDO;
import cn.iocoder.yudao.module.system.dal.dataobject.worksaleinventory.WorkSaleInventoryDO;
import cn.iocoder.yudao.module.system.dal.mysql.workorder.WorkOrderMapper;
import cn.iocoder.yudao.module.system.dal.mysql.worksaleinventory.WorkSaleInventoryMapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import cn.iocoder.yudao.module.system.controller.admin.workhanging.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.workhanging.WorkHangingDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.system.dal.mysql.workhanging.WorkHangingMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 下挂记录 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class WorkHangingServiceImpl implements WorkHangingService {

    @Resource
    private WorkOrderMapper workOrderMapper;
    @Resource
    private WorkHangingMapper workHangingMapper;
    @Resource
    private WorkSaleInventoryMapper workSaleInventoryMapper;

    @Override
    public Integer createWorkHanging(WorkHangingSaveReqVO createReqVO) {
        Long userId = WebFrameworkUtils.getLoginUserId();
        WorkOrderDO workOrderData = workOrderMapper.selectById(createReqVO.getWorkId());
        //更新订单信息
        UpdateWrapper<WorkOrderDO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",createReqVO.getWorkId());
        BigDecimal znum = createReqVO.getTotalQuantity().add(createReqVO.getRejectsNum());
        if(workOrderData.getStorageMethod().equals("1")){
            updateWrapper.setSql("hanging_uid="+userId+",racking_quantity = racking_quantity - "+znum+",hanging_quantity = hanging_quantity + "+znum+",cp_defect_num = cp_defect_num + "+createReqVO.getRejectsNum()+",finished_product_num = finished_product_num + "+createReqVO.getTotalQuantity());
        }else{
            updateWrapper.setSql("hanging_uid="+userId+",racking_quantity = racking_quantity - "+znum+",hanging_quantity = hanging_quantity + "+znum+",cp_defect_num = cp_defect_num + "+createReqVO.getRejectsNum());
        }
        workOrderMapper.update(updateWrapper);
        // 插入
        WorkHangingDO workHanging = BeanUtils.toBean(createReqVO, WorkHangingDO.class);
        workHanging.setCustomerId(workOrderData.getCustomerId());
        workHangingMapper.insert(workHanging);


        // 插入
        WorkSaleInventoryDO workSaleInventory = new WorkSaleInventoryDO();
        workSaleInventory.setWorkId(workHanging.getWorkId());
        workSaleInventory.setCustomerId(workOrderData.getCustomerId());
        workSaleInventory.setBoxNum(workHanging.getBoxNum());
        workSaleInventory.setPerBoxNum(workHanging.getPerBoxNum());
        workSaleInventory.setQuantity(workHanging.getTotalQuantity().intValue());
        workSaleInventory.setWeight(new BigDecimal(0));
        workSaleInventory.setDate(LocalDateTime.now());
        workSaleInventoryMapper.insert(workSaleInventory);
        // 返回
        return workHanging.getId();
    }

    @Override
    public void updateWorkHanging(WorkHangingSaveReqVO updateReqVO) {
        // 校验存在
        validateWorkHangingExists(updateReqVO.getId());
        // 更新
        WorkHangingDO updateObj = BeanUtils.toBean(updateReqVO, WorkHangingDO.class);
        workHangingMapper.updateById(updateObj);
    }

    @Override
    public void deleteWorkHanging(Integer id) {
        // 校验存在
        validateWorkHangingExists(id);
        // 删除
        workHangingMapper.deleteById(id);
    }

    @Override
        public void deleteWorkHangingListByIds(List<Integer> ids) {
        // 校验存在
        validateWorkHangingExists(ids);
        // 删除
        workHangingMapper.deleteByIds(ids);
        }

    private void validateWorkHangingExists(List<Integer> ids) {
        List<WorkHangingDO> list = workHangingMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(WORK_HANGING_NOT_EXISTS);
        }
    }

    private void validateWorkHangingExists(Integer id) {
        if (workHangingMapper.selectById(id) == null) {
            throw exception(WORK_HANGING_NOT_EXISTS);
        }
    }

    @Override
    public WorkHangingDO getWorkHanging(Integer id) {
        return workHangingMapper.selectById(id);
    }

    @Override
    public PageResult<WorkHangingDO> getWorkHangingPage(WorkHangingPageReqVO pageReqVO) {
        return workHangingMapper.selectPage(pageReqVO);
    }

}
